﻿using AutoMapper;
using B.S.Outsourcing.Domain;
using B.S.Outsourcing.ErrorCode;
using B.S.Outsourcing.Infatructrue;
using B.S.Outsourcing.Writer.Api.Application.Command;
using MediatR;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace B.S.Outsourcing.Writer.Api.Application.CommandHandler
{
    public class CreateOutsourcingCommandHandler : IRequestHandler<CreateOutsourcingCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<OutsourcingModel> outReps;
        private readonly IMapper mapper;
        private readonly DBHelper1 dbHelper;
        public CreateOutsourcingCommandHandler(IBaseRepository<OutsourcingModel> outReps, IMapper mapper, DBHelper1 dbHelper = null)
        {
            this.outReps = outReps;
            this.mapper = mapper;
            this.dbHelper = dbHelper;
        }

        public async Task<ApiResult<int>> Handle(CreateOutsourcingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var model = $"insert into OutsourcingModel values('{request.Name}','{request.Abbreviation}','{request.Status}','{request.Address}','{request.PostalCode}','{request.Telephone}','{request.Remarks}','{request.ContactPerson}','{request.Mobile}','{request.Bank}','{request.BankAddressPhone}','{request.AccountNumber}','{request.TaxId}','{request.IsDelete}')";
            var str = dbHelper.NonQuery(model);
            if (str > 0)
            {
                result.Code = ApiEnums.成功;
                result.Message = "成功";
                result.Data = str;
            }
            else
            {
                result.Code = ApiEnums.失败;
                result.Message = "失败";
                result.Data = str;
            }
            return await Task.FromResult(result);
           
        }
    }
}
